package jsonTest;

import java.io.IOException;
import java.util.List;

import org.json.JSONException;

import com.ruiyi.error.RuiyiCredentialsException;
import com.ruiyi.error.RuiyiError;
import com.ruiyi.error.RuiyiException;

import json.Json_Account;
import json.Json_Company;
import json.Json_Product;
import util.LogUtil;
import handler.Handler;
import handler.Message;

public class Mgr {
	protected static final String TAG = "Mgr\t\t";
	private static Mgr mgr;

	private JsonUtil util;

	public Mgr() {
		mgr = this;
		util = new JsonUtil();
	}

	public static Mgr getInstance() {
		if (mgr == null)
			mgr = new Mgr();
		return mgr;
	}

	OnDataReady dataReady;
	final Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				LogUtil.d(TAG, "" + msg.what);
				dataReady.setData(msg.obj);
				break;
			case 1:
				LogUtil.d(TAG, "" + msg.what);
				break;
			}
		}
	};
	
	private void executeCmd(final Command cmd) {
		Thread t = new Thread(new Runnable(){
			@Override
			public void run() {
				cmd.execute();
				return;
			}
			
		});
		t.start();
		
//		Runnable r = new Runnable() {
//			@Override
//			public void run() {
//				cmd.execute();
//				return;
//			}
//		};
//		r.run();
	}

	private void cancleCmd(){
		
	}
	
	public void login(final String name, final String pswd,
			final OnDataReady dataReady) {
		this.dataReady = dataReady;

		LoginCommand logincmd = new LoginCommand(name, pswd);

		executeCmd(logincmd);
	}

	//*********************************************************************************************

	class LoginCommand implements Command {
		String user, password;

		public LoginCommand(String user, String password) {
			this.user = user;
			this.password = password;
		}

		@Override
		public void execute() {
			try {
				Object obj = util.login(user, password);

				if(handler!=null)
					handler.sendMessage(Message.Obtain(handler, 0, obj));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	class LogOutCommand implements Command {

		public LogOutCommand() {
		}

		@Override
		public void execute() {
//			login.logout();
		}
	}
	//*********************************************************************************************
	class MyCompany extends Company{
		Json_Account account;
		public MyCompany(Json_Account account, Json_Company company, OnDataReady dataReady2) {
			super(company, dataReady2);
			this.account = account;
		}
		public MyCompany(Json_Company company, OnDataReady dataReady2) {
			super(company, dataReady2);
		}
	}
	//*********************************************************************************************
	class Company {
		Json_Company company;
		public OnDataReady dataReady;

		public Company(Json_Company company, OnDataReady dataReady2) {
			this.company = company;
			dataReady = dataReady2;
		}
	}
	class GetProductListCommand implements Command {	
		Company com;

		public GetProductListCommand(Company company) {
			this.com = company;
		}

		@Override
		public void execute() {
			try {
				List<Json_Product> products = util.getProductList(com.company.getId());
				if (dataReady != null)
					dataReady.setData(products);
			} catch (Exception e) {
				e.printStackTrace();
				if (dataReady != null)
					dataReady.setData(null);
			}
		}
	}
	//*********************************************************************************************
	public interface OnDataReady {
		public void setData(Object obj);
	}
	//*********************************************************************************************
}